package edu.mbb.ioc;

import java.lang.reflect.Field;
import java.util.Set;

import edu.mbb.annocation.Autowried;

public class DIClass {
	public static void Di() {
		Set<Class<?>> classSet = ClassNames.getClassSet();
		if (classSet == null || classSet.size() == 0) {
			return;
		}
		for (Class<?> class1 : classSet) {
			Field[] declaredFields = class1.getDeclaredFields();
			for (Field field : declaredFields) {
				if (field.isAnnotationPresent(Autowried.class)) {
					Object bean = BeanFactory.getBean(class1.getSimpleName());
					if (bean == null) {
						System.out.println("注入失败");
						return;
					}
					field.setAccessible(true);
					Class<?> type = field.getType();
					String simpleName = type.getSimpleName();
					try {
						field.set(bean, BeanFactory.getBean(simpleName));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
						System.out.println("注入失败");
					} catch (IllegalAccessException e) {
						e.printStackTrace();
						System.out.println("注入失败");
					}
				}
			}
		}
	}
}
